//
//  SLLockDetailView.m
//  SlockApp
//
//  Created by 凯朱 on 2024/7/25.
//

#import "SLLockDetailView.h"

@implementation SLLockDetailView
-(void)awakeFromNib{
    [super awakeFromNib];
    _listBgView.layer.cornerRadius = 20;
    _listBgView.layer.masksToBounds = YES;
    
    [_unlockBtn setTitle:kLocalString(@"Unlock") forState:UIControlStateNormal];
    [_syscBtn setTitle:kLocalString(@"Sync") forState:UIControlStateNormal];
    
    
    _listLbl1.text = kLocalString(@"Access management");
    _listLbl2.text = kLocalString(@"Activity logs");
    _listLbl3.text = kLocalString(@"Lock settings");
    
    _unlockBtn.btnStyle = 1;
    _syscBtn.btnStyle = 2;
    
    _listView1Height.constant = 50;
    _bgViewHeight.constant = 150;
    _descLbl1.hidden = YES;
    
    kWeakSelf(self);
    _listView1.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        if (weakself.model.isGuest) {
            return;
        }
        if (weakself.btnBlock) weakself.btnBlock(kLocalString(@"Access management"));
    }];
    [_listView1 addGestureRecognizer:tap1];
    
    _listView2.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        if (weakself.model.isGuest) {
            if (weakself.btnBlock) weakself.btnBlock(kLocalString(@"Delete this access"));
        }else{
            if (weakself.btnBlock) weakself.btnBlock(kLocalString(@"Activity logs"));
        }
    }];
    [_listView2 addGestureRecognizer:tap2];
    
    _listView3.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        if (weakself.btnBlock) weakself.btnBlock(kLocalString(@"Lock settings"));
    }];
    [_listView3 addGestureRecognizer:tap3];
    _animation = [AnimationLoader loadAnimationRepeatFrom:@"UnlockingCrle" into:self.animationView];
}

-(void)setModel:(SLLockModel *)model{
    _model = model;
    if (!model.isGuest) {
        return;
    }
    _listView3.hidden = YES;
    _descLbl1.hidden = NO;
    _bgView3Height.constant = 0;
    _listView1Height.constant = 67;
    _bgViewHeight.constant = 117;
    _listLine2.hidden = YES;
    _listLbl1.text = kLocalString(@"Access claimed by");
    _icon1.hidden = YES;
    _listLbl2.text = kLocalString(@"Delete this access");
    _descLbl1.text = _model.createBy;
    _temptureLbl.hidden = YES;
    _temptureIcon.hidden = YES;
    _batteryCenterX.constant = 35;
    
}

-(void)playAnimation{
    [AnimationLoader playAnimationAndStopWithAnimationView:_animation for:2];
}

-(void)stopPlayAnimation{
    
}

- (void)createCircularProgressWithRadius:(CGFloat)radius strokeWidth:(CGFloat)strokeWidth fillColor:(UIColor *)fillColor backgroundColor:(UIColor *)backgroundColor progress:(CGFloat)progress {
    CGFloat startAngle = -M_PI_2;
    CGFloat endAngle = startAngle + (2 * M_PI * progress);
    
    // 创建背景圆形路径
    UIBezierPath *backgroundPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(radius, radius)
                                                                  radius:radius - (strokeWidth / 2.0)
                                                              startAngle:0
                                                                endAngle:(2 * M_PI)
                                                               clockwise:YES];
    // 创建填充圆形路径
    UIBezierPath *progressPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(radius, radius)
                                                                radius:radius - (strokeWidth / 2.0)
                                                            startAngle:startAngle
                                                              endAngle:endAngle
                                                             clockwise:YES];
    
    // 创建背景圆形图层
    CAShapeLayer *backgroundLayer = [CAShapeLayer layer];
    backgroundLayer.path = backgroundPath.CGPath;
    backgroundLayer.lineWidth = strokeWidth;
    backgroundLayer.strokeColor = backgroundColor.CGColor;
    backgroundLayer.fillColor = [UIColor clearColor].CGColor;
    
    // 创建填充圆形图层
    CAShapeLayer *progressLayer = [CAShapeLayer layer];
    progressLayer.lineCap = kCALineCapRound;
    progressLayer.path = progressPath.CGPath;
    progressLayer.lineWidth = strokeWidth;
    progressLayer.strokeColor = fillColor.CGColor;
    progressLayer.fillColor = [UIColor clearColor].CGColor;
    
    // 添加图层到视图中
    [_bgView.layer addSublayer:backgroundLayer];
    [_bgView.layer addSublayer:progressLayer];
}


- (IBAction)btnAction:(UIButton *)sender {
    if (self.btnBlock) self.btnBlock(sender.titleLabel.text);
}

@end
